{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3b07da7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.b3c5e707d1b22b0711aac081c3bdf0ba.2592000.1652270227.282335-25946157'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取access_token\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'Lt9uhnN79YWWOS3DMfLFv43f',\n",
    "    'client_secret':'s3UChPTA7VIs4pT4zF3p9CyaGBp53TNC'\n",
    "}\n",
    "\n",
    "response = requests.get(host,params=payload)\n",
    "access_token=response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "08e62a20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '义务教育课程标准实验教科书'}, {'words': '数学'}, {'words': 'SHUXUE'}, {'words': '五年级'}, {'words': '下册'}, {'words': '课程教材研究所'}, {'words': '编著'}, {'words': '小学数学课程教材研究开发中心'}], 'words_result_num': 8, 'log_id': 1513486313855734569}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('课本.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b613d49b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['百度大脑'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513486828680485077}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('二维码.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "be6db32a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '春宵苦短,'}, {'words': '少女前进吧!'}], 'words_result_num': 2, 'log_id': 1513487788673698907}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('网络图片.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "649c7f9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 96, 'left': 197, 'width': 107, 'height': 49}, 'words': '徐乐'}, '民族': {'location': {'top': 181, 'left': 389, 'width': 30, 'height': 43}, 'words': '汉'}, '住址': {'location': {'top': 336, 'left': 184, 'width': 398, 'height': 99}, 'words': '安徽省宿州市埇桥区朱仙庄镇'}, '公民身份号码': {'location': {'top': 530, 'left': 333, 'width': 541, 'height': 60}, 'words': '652901196611026716'}, '出生': {'location': {'top': 258, 'left': 188, 'width': 315, 'height': 41}, 'words': '19661102'}, '性别': {'location': {'top': 181, 'left': 184, 'width': 39, 'height': 44}, 'words': '男'}}, 'words_result_num': 6, 'idcard_number_type': 1, 'image_status': 'normal', 'log_id': 1513488648609508723}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('身份证.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "caafd392",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '壹佰玖拾捌圆整', 'InvoiceNumConfirm': '56965075', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '98.01980198'}], 'NoteDrawer': '杨梦霞', 'SellerAddress': '百色市右江区城乡路卫校斜对面0776-2869999', 'CommodityNum': [{'row': '1', 'word': '2'}], 'SellerRegisterNum': '91451000552284945M', 'MachineCode': '', 'Remarks': '', 'SellerBank': '广西百色右江农村合作银行银兴支行604112010101357786', 'CommodityTaxRate': [{'row': '1', 'word': '1%'}], 'TotalTax': '1.96', 'InvoiceCodeConfirm': '045001900104', 'CheckCode': '55475185954042453339', 'InvoiceCode': '045001900104', 'InvoiceDate': '2020年06月05日', 'PurchaserRegisterNum': '914501006777146113', 'InvoiceTypeOrg': '广西增值税普通发票', 'OnlinePay': '', 'Password': '>2+-/8736</<-/26-><4536/829-39-275*>628>13*0*->60<4072>2*4484+8<3/9+/3-/639<<*5->*921<1--2-3-<7<8<72*40+9>3<', 'Agent': '否', 'AmountInFiguers': '198.00', 'PurchaserBank': '', 'Checker': '黄理县', 'City': '', 'TotalAmount': '196.04', 'CommodityAmount': [{'row': '1', 'word': '196.04'}], 'PurchaserName': '南宁广发重工发电设备有限责任公司', 'CommodityType': [], 'Province': '广西', 'InvoiceType': '普通发票', 'SheetNum': '第一联：记账联', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '1.96'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '间'}], 'Payee': '宁玉梅', 'CommodityName': [{'row': '1', 'word': '住宿服务*住宿费'}], 'SellerName': '百色市豪瑞酒店有限公司', 'InvoiceNum': '56965075'}, 'log_id': 1513489525585937141}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('增值税发票.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "220c0619",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
